package com.liyuxiang.yygh.order.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liyuxiang.yygh.common.result.Result;
import com.liyuxiang.yygh.common.utils.AuthContextHolder;
import com.liyuxiang.yygh.enums.OrderStatusEnum;
import com.liyuxiang.yygh.model.order.OrderInfo;
import com.liyuxiang.yygh.order.service.OrderService;
import com.liyuxiang.yygh.vo.order.OrderCountQueryVo;
import com.liyuxiang.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Autowired
    OrderService orderService;

    //生成挂号订单
    @ApiOperation("生成订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrder(@ApiParam(name = "scheduleId", value = "排班id", required = true) @PathVariable("scheduleId") String scheduleId, @ApiParam(name = "patientId", value = "就诊人id", required = true) @PathVariable("patientId") Long patientId) {
        return Result.ok(orderService.saveOrder(scheduleId, patientId));
    }

    @ApiOperation("根据订单id查询订单")
    @GetMapping("auth/getOrderInfo/{id}")
    public Result getOrderInfo(@ApiParam(name = "id", value = "订单id", required = true) @PathVariable("id") Long id) {
        return Result.ok(orderService.getOrderById(id));
    }

    @ApiOperation("取消订单")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@ApiParam(name = "orderId", value = "订单id", required = true) @PathVariable("orderId") Long orderId) {
        return orderService.cancelOrder(orderId) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据用户获取订单列表")
    @GetMapping("auth/{page}/{limit}")
    public Result getOrderPageByUserId(@PathVariable("page") Long page, @PathVariable("limit") Long limit, OrderQueryVo orderQueryVo, HttpServletRequest request) {
        return Result.ok(orderService.getOrderPageByUserId(new Page<>(page, limit), orderQueryVo, AuthContextHolder.getUserId(request)));
    }

    @ApiOperation("获取订单支付状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation("获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }

}
